package com.weiiew.itemtypeservice.service;

import com.weiiew.itemtypeservice.mapper.ItemTypeMapper;
import com.weiiew.common.entity.dto.ItemType;
import com.weiiew.common.entity.simple.SimpleType;
import com.weiiew.common.entity.vo.ItemTypeVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestHeader;

import java.util.List;

/**
 * @Description
 * @Author 魏锴
 * @Date 2024/8/6
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class ItemTypeServiceImpl implements ItemTypeService {
    private final ItemTypeMapper itemTypeMapper;

    @Override
    public List<SimpleType> getSimpleItemTypes(Integer version) {
        return itemTypeMapper.getSimpleItemTypes(version);
    }

    @Override
    public List<ItemTypeVO> getItemTypes(String name, @RequestHeader("my-version") Integer version) {
        return itemTypeMapper.getItemTypes(name, version);
    }

    @Override
    public boolean createType(ItemType type) {
        return itemTypeMapper.insertType(type);
    }

    @Override
    public boolean renameType(Integer id, String name) {
        return itemTypeMapper.renameType(id, name);
    }

    @Override
    public boolean removeType(Integer id) {
        if (itemTypeMapper.countItemByType(id) != 0) return false;
        return itemTypeMapper.deleteType(id);
    }

}
